home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / CoreGateway / GatewayConfig.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  10.2 KB  |  385 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        GatewayConfig.cp
  3.  
  4.     Copyright:    Â© 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef __BLJSTANDARDINCLUDES__
  15. #include "BLJStandardIncludes.h"
  16. #endif
  17.  
  18. #ifndef __BLJOCEUTILITIES__
  19. #include "BLJOCEUtilities.h"
  20. #endif
  21.  
  22. #ifndef __DEBUGGINGGEAR__
  23. #include "DebuggingGear.h"
  24. #endif
  25.  
  26. #ifndef __GATEWAYCONFIG__
  27. #include "GatewayConfig.h"
  28. #endif
  29.  
  30. #ifndef __ADASTUPLEDATABASE__
  31. #include "ADASTupleDatabase.h"
  32. #endif
  33.  
  34. #ifndef __OCESTANDARDDIRECTORY__
  35. #include "OCEStandardDirectory.h"
  36. #endif
  37.  
  38. #ifndef __UTILITIES__
  39. #include "Utilities.h"
  40. #endif
  41.  
  42. #ifndef __STDGETDIR__
  43. #include "StdGetDir.h"
  44. #endif
  45.  
  46.  
  47. static TADASTupleDatabase* gConfigurationDB = nil;
  48.  
  49. //    This returns the current configuration database, or nil if there is none.
  50. TADASTupleDatabase* GetConfigurationDB()
  51. {
  52.     return gConfigurationDB;
  53. }
  54.  
  55. typedef struct {
  56.     unsigned long        version;
  57.     OSType                signature;
  58.  
  59.     PackedRecordID        packedAdministratorRecordID;
  60.     AuthKey                authenticationKey;
  61.  
  62.     PackedDSSpec        configurationPackedRecordID;
  63. } ConfigPreferencesDataRecord, **ConfigPreferencesDataHandle;
  64.  
  65. const long kConfigPreferencesFileVersion = 1;
  66. const OSType kConfigPreferencesSignature = 'BovJ';
  67.  
  68. #pragma segment GatewayConfig
  69.  
  70. Handle ReadFileIntoHandle (FSSpec fileSpec) {
  71.  
  72.     Handle dataH = nil;
  73.     short refNum = 0;
  74.  
  75.     TRY
  76.         FAILOSErr(FSpOpenDF(&fileSpec, fsWrPerm, &refNum));
  77.  
  78.         long fileSize;
  79.         FAILOSErr( GetEOF (refNum, &fileSize));
  80.  
  81.         dataH = FAILNewHandle(fileSize);
  82.         FAILNULL(dataH);
  83.  
  84.         HLock((Handle) dataH);
  85.         FAILOSErr(FSRead(refNum, &fileSize, (Ptr) *dataH));
  86.  
  87.         FAILOSErr(FSClose(refNum));
  88.  
  89.     EXCEPTION
  90.         if (dataH)
  91.             DeallocateHandle(dataH);
  92.         dataH = nil;
  93.  
  94.         if (refNum)
  95.             FSClose(refNum);
  96.  
  97.     ENDEXCEPTION;
  98.  
  99.     return dataH;
  100. };
  101.  
  102.  
  103.  
  104. void DisplayConfigDB();
  105.  
  106. //    This function 'sets up' the configuration database.  The whichDB parameters should be
  107. //    the name of a file of type 'pref', creator 'BOVJ', located in the boot volume's
  108. //    preferences folder.  This file contains the information which is needed to
  109. //    actually bootstrap the configuration database.  If the configuration database
  110. //    can correctly be set up, then this function returns true, otherwise it returns false.
  111.  
  112. Boolean SetupConfigurationDB (short vRefNum, long dirID, Str63 whichDB) {
  113.     ConfigPreferencesDataHandle dataH = nil;
  114.  
  115. #if debug
  116.     if (gConfigurationDB != nil) {
  117.         keith << "SetupConfigurationDB called, but a configDB already exists." << endl << flush;
  118.     };
  119. #endif
  120.     TRY
  121.         FSSpec fileSpec;
  122.         FAILOSErr(FSMakeFSSpec(vRefNum, dirID, whichDB, &fileSpec));
  123.  
  124.         dataH = (ConfigPreferencesDataHandle) ReadFileIntoHandle (fileSpec);
  125.         FAILNULL(dataH);
  126.  
  127.         FAILifFALSE(OCEValidPackedRecordID(&(**dataH).packedAdministratorRecordID));
  128.  
  129.         RecordID userRecord;
  130.         OCEUnpackRecordID (&(**dataH).packedAdministratorRecordID, &userRecord);
  131.  
  132.         AuthKey key = (**dataH).authenticationKey;
  133.  
  134.         //    Create an identity, using the recordID and key from the preferences file.
  135.         AuthIdentity identity = 0;
  136.         {    AuthBindIdentityPB bindIdentityPB;
  137.  
  138.             memset(&bindIdentityPB, 0, sizeof(bindIdentityPB));
  139.             bindIdentityPB.dsRefNum = 0;
  140.             bindIdentityPB.userIdentity = 0;
  141.             bindIdentityPB.userRecord = &userRecord;
  142.             bindIdentityPB.userKey = &key;
  143.  
  144.             keith << "CreateID: user=" << userRecord << " key="; keith.write((char*) &key, (int) sizeof(key)); keith << endl << flush;
  145.  
  146.             OSErr err = AuthBindIdentity((AuthParamBlockPtr) &bindIdentityPB, false);
  147.  
  148.             keith << "SetupConfigurationDB, creating identity with ASCreateIdentity(), id:" << bindIdentityPB.userIdentity << " err=" << err << endl;
  149.  
  150.             if ((err != noErr) && (err != daIdentityExists))
  151.                 FAILOSErr(err);
  152.  
  153.             identity = bindIdentityPB.userIdentity;
  154.         };
  155.  
  156.         FAILifFALSE(OCEValidPackedDSSpec(&(**dataH).configurationPackedRecordID));
  157.  
  158.         DSSpec    spec;
  159.         RecordID configurationRecord;
  160.         OCEUnpackDSSpec(&(**dataH).configurationPackedRecordID, &spec, &configurationRecord);
  161.  
  162.         //    Now, create the configuration database.  Finally.
  163.         gConfigurationDB = new TADASTupleDatabase();
  164.         FAILNULL(gConfigurationDB);
  165.  
  166.         FAILifFALSE( ((TADASTupleDatabase*) gConfigurationDB)->IADASTupleDatabase(identity, 0, &configurationRecord, nil));
  167.  
  168.         DeallocateHandle((Handle) dataH);
  169.  
  170. //        DisplayConfigDB();
  171.  
  172.     EXCEPTION
  173.         DeallocateHandle((Handle) dataH);
  174.         delete gConfigurationDB;
  175.         gConfigurationDB = nil;
  176.  
  177.     ENDEXCEPTION;
  178.  
  179.     return (gConfigurationDB != nil);
  180. };
  181.  
  182. OSErr WriteHandleToFile (FSSpec fileSpec, Handle dataH) {
  183.     OSErr err = noErr;
  184.     short refNum = 0;
  185.  
  186.     TRY
  187.         err = FSpCreate (&fileSpec, 'pref', 'BOVJ', smRoman);
  188.         if ((err != noErr) && (err != dupFNErr))
  189.             FAILOSErr(err);
  190.  
  191.         FAILOSErr(FSpOpenDF(&fileSpec, fsWrPerm, &refNum));
  192.  
  193.         HLock((Handle) dataH);
  194.         long dataSize = GetHandleSize((Handle) dataH);
  195.         FAILOSErr(FSWrite(refNum, &dataSize, (Ptr) *dataH));
  196.  
  197.         FAILOSErr(FSClose(refNum));
  198.  
  199.     EXCEPTION
  200.         if (refNum)
  201.             FSClose(refNum);
  202.         err = EXCEPTIONCODE;
  203.  
  204.     ENDEXCEPTION;
  205.  
  206.     return noErr;
  207. };
  208.  
  209.  
  210. //    This function creates a configuration file in the local preferences file (see the
  211. //    SetupConfigurationDB function).  This function does not set up the configuration
  212. //    database.  If the preferences file is successfully created, then this function
  213. //    returns true, otherwise it returns false.
  214.  
  215. Boolean CreateConfigurationDB(short vRefNum, long dirID, Str63 whichDB) {
  216.     Boolean result = false;
  217.     ConfigPreferencesDataHandle dataH = nil;
  218.     short refNum = 0;
  219.     RecordID recordID;
  220.  
  221.     recordID.rli = nil;
  222.     recordID.local.recordName = nil;
  223.     recordID.local.recordType = nil;
  224.  
  225.     TRY
  226.         //    Now, create the data that gets written to the preferences file.
  227.         dataH = (ConfigPreferencesDataHandle) FAILNewHandle( sizeof(**dataH));
  228.         (**dataH).version = kConfigPreferencesFileVersion;
  229.         (**dataH).signature = kConfigPreferencesSignature;
  230.  
  231.         RString password;
  232.         AuthIdentity identity;
  233.         DirectoryName directoryName;
  234.         DirDiscriminator dirDiscriminator;
  235.  
  236.         FAILOSErr(SDPPromptForIdentityAndPassword(&identity, &password, nil, "\pLog-on as the Gateway Administrator…", false,
  237.                                                 &directoryName, &dirDiscriminator));
  238.  
  239.         {    //    Allocate the fields in the recordID
  240.             memset (&recordID, 0, sizeof(recordID));
  241.             recordID.rli = (PackedRLIPtr) FAILNewPtr(kRLIMaxBytes);
  242.             recordID.local.recordName = (RStringPtr) FAILNewPtr(sizeof(RString));
  243.             recordID.local.recordName->dataLength = kRStringMaxBytes;
  244.             recordID.local.recordType = (RStringPtr) FAILNewPtr(sizeof(RString));
  245.             recordID.local.recordType->dataLength = kRStringMaxBytes;
  246.         };
  247.  
  248.  
  249.         {    AuthGetIdentityInfoPB getIdentityInfoPB;
  250.  
  251.             memset(&getIdentityInfoPB, 0, sizeof(getIdentityInfoPB));
  252.             getIdentityInfoPB.userIdentity = identity;
  253.             getIdentityInfoPB.userRecord = &recordID;
  254.             FAILOSErr(AuthGetIdentityInfo((AuthParamBlockPtr) &getIdentityInfoPB, false));
  255.         };
  256.  
  257.         OCEPackRecordID (&recordID, &(**dataH).packedAdministratorRecordID, sizeof((**dataH).packedAdministratorRecordID));
  258.  
  259.         keith << "ASPromptForRID: RecordID = " << recordID << " pwd=" << password << endl;
  260.  
  261.         {    AuthPasswordToKeyPB passwordToKeyPB;
  262.             AuthKey        key;
  263.  
  264.             memset(&passwordToKeyPB, 0, sizeof(passwordToKeyPB));
  265.             passwordToKeyPB.key = &key;
  266.             passwordToKeyPB.password = &password;
  267.  
  268.             FAILOSErr( AuthPasswordToKey((AuthParamBlockPtr) &passwordToKeyPB, false));
  269.             keith << "ASPwdToKey: key='"; keith.write((char *) &key, sizeof(key)); keith << "'" << endl;
  270.             (**dataH).authenticationKey = key;
  271.         };
  272.  
  273.  
  274.         //    Now, have the user find the configuration record for this gateway, using the CollabPack SDGetDirectories window
  275.         PackedDSSpecPtr    recordFound = nil;
  276.         TRY
  277.             RString type;
  278.             RStringPtr typesArray[1];
  279.  
  280.             OCECToRString("ApplelinkGwyCnfg", smRoman, &type, kRStringMaxBytes);
  281.             typesArray[0] = &type;
  282.  
  283.             FAILOSErr(SDPGetDirectories(&recordFound, nil, typesArray, 1, "\pSelect the gateway configuration…", identity));
  284.  
  285.         EXCEPTION
  286.             DeallocatePtr((Ptr) recordFound);
  287.  
  288.         ENDEXCEPTION;
  289.  
  290.         OCECopyPackedDSSpec(recordFound, &(**dataH).configurationPackedRecordID, sizeof((**dataH).configurationPackedRecordID));
  291.  
  292.         //    Now, make a file in the given folder, and put this goofy data into it.
  293.         FSSpec fileSpec;
  294.         {    OSErr err = FSMakeFSSpec(vRefNum, dirID, whichDB, &fileSpec);
  295.             if ((err != noErr) && (err != fnfErr))
  296.                 FAILOSErr(err);
  297.         };
  298.         FAILOSErr(WriteHandleToFile(fileSpec, (Handle) dataH));
  299.  
  300.         //    We're done!
  301.  
  302.         DeallocatePtr((Ptr) recordID.rli);
  303.         DeallocatePtr((Ptr) recordID.local.recordName);
  304.         DeallocatePtr((Ptr) recordID.local.recordType);
  305.  
  306.         DeallocateHandle((Handle) dataH);
  307.  
  308.         result = true;
  309.  
  310.     EXCEPTION
  311.         DeallocatePtr((Ptr) recordID.rli);
  312.         DeallocatePtr((Ptr) recordID.local.recordName);
  313.         DeallocatePtr((Ptr) recordID.local.recordType);
  314.  
  315.         DeallocateHandle( (Handle) dataH);
  316.  
  317.         result = false;
  318.  
  319.     ENDEXCEPTION;
  320.  
  321.     return result;
  322. };
  323.  
  324.  
  325.  
  326. #if debug
  327. void DisplayConfigDB() {
  328.     TADASTupleDatabase* db = GetConfigurationDB();
  329.  
  330.     if (db) {
  331.         long count = db->CountTuples();
  332.  
  333.         for (long index = 1; index <= count; ++index) {
  334.             long keySize = 64, dataSize = 768, actualDataSize;
  335.             char key[64], data[768];
  336.  
  337.             if (db->GetIndexTuple(index, (void*) key, keySize, (void*) data, dataSize)) {
  338.                     keith << " #" << index << " key="; keith.write(key, (int) keySize);
  339.                     keith << " data=(" << dataSize << ") "; keith.write(data, (int) shortmin(32, dataSize)); keith << endl << flush;
  340.             } else {
  341.                 keith << " #" << index << " could not be loaded, keySize=" << keySize << endl;
  342.             };
  343.         };
  344.     } else {
  345.         keith << "DisplayConfigDB: no configDB exists." << endl << flush;
  346.     };
  347. };
  348. #endif
  349.  
  350.  
  351.  
  352. #if debug
  353.  
  354. //    This function creates a 'special' preferences file which will use a local (non-ADAS)
  355. //    database instead of the ADAS database setup by the standard CreateConfigurationDB().
  356.  
  357. Boolean CreateLocalConfigurationDB(Str63 whichDB);
  358.  
  359. #endif
  360.  
  361.  
  362. //    These are a bunch of 'short-hand' functions for getting certain types of data out
  363. //    of the configuration database.
  364.  
  365. Boolean GetConfigRString (char* itemName, RString& item);
  366. Boolean GetConfigLong (char* itemName, long& item);
  367.  
  368.  
  369.  
  370. Boolean GetConfigCString (char* itemName, char* buffer, short maxBufferSize) {
  371.     TADASTupleDatabase* db = GetConfigurationDB();
  372.     if (db) {
  373.         long dataLength;
  374.         if (db->GetTupleSize((void*) itemName, strlen(itemName), dataLength))
  375.             if (dataLength+1 < maxBufferSize) {
  376.                 long actualDataSize = 0;
  377.                 if (db->GetTuple((void*) itemName, strlen(itemName), actualDataSize, (void*) buffer, maxBufferSize)) {
  378.                     buffer[dataLength] = 0;
  379.                     return true;
  380.                 };
  381.         };
  382.     };
  383.     return false;
  384. };
  385.